/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vendingmachine;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Nama File    : CoinStorage.Java
 * Deskripsi    : deskripsi dan implementasi kelas CoinStorage yang merupakan tempat penyimpanan koin yang mempunyai ukuran tertentu
 * Nama Pembuat : Muhammad Ghufron Mahfudhi (23512066) dan Jonathan Ery Pradana (23512038)
 * Hari/Tanggal : Senin / 26 November 2012
 */
public class CoinStorage {
    //atribut
    //private List<Coin> coins;
    
    private List<Coin> nickelTray;
    private List<Coin> dimeTray;
    private List<Coin> quarterTray;
    private int size;
    
    //method
    
    //ctor
    public CoinStorage() {
        this.nickelTray = new ArrayList<>();
        this.dimeTray = new ArrayList<>();
        this.quarterTray = new ArrayList<>();
        size = 0;
    }

    //ctor berparameter
    public CoinStorage(List<Coin> nickel, List<Coin> dime, List<Coin> quarter, int size) {
        this.nickelTray = new ArrayList<>(nickel);
        this.dimeTray = new ArrayList<>(dime);
        this.quarterTray = new ArrayList<>(quarter);
        this.size = size;
    }
    
    public CoinStorage(int size) {
        this.nickelTray = new ArrayList<>(size);
        this.dimeTray = new ArrayList<>(size);
        this.quarterTray = new ArrayList<>(size);
        this.size = size;
    }

    //getter
    public List<Coin> getNickelTray() {
        return nickelTray;
    }

    public List<Coin> getDimeTray() {
        return dimeTray;
    }

    public List<Coin> getQuarterTray() {
        return quarterTray;
    }
    
    public int getSize() {
        return size;
    }

    //setter

    public void setNickelTray(List<Coin> nickelTray) {
        this.nickelTray.clear();
        this.nickelTray.addAll(nickelTray);
    }

    public void setDimeTray(List<Coin> dimeTray) {
        this.dimeTray.clear();
        this.dimeTray.addAll(dimeTray);
    }

    public void setQuarterTray(List<Coin> quarterTray) {
        this.quarterTray.clear();
        this.quarterTray.addAll(quarterTray);
    }
    
    public void setSize(int size) {
        this.size = size;
    }
    
    public List<Coin> generateChangeCoin(PaymentCounter payment){
        List<Coin> resultCoins = new ArrayList<>();
        if(payment.getChange() >= 25){
            int NQuarter = payment.getChange() / 25;
            for(int i = 0; i < NQuarter; i++){
                resultCoins.add(this.quarterTray.remove(quarterTray.size() - 1));
            }

            if((payment.getChange() % 25) >= 10){
                int NDime = (payment.getChange() % 25) / 10;
                for(int i = 0; i < NDime; i++){
                    resultCoins.add(this.dimeTray.remove(dimeTray.size() - 1));
                }

                if(((payment.getChange() % 25) % 10) >= 5){
                    int NNickel = ((payment.getChange() % 25) % 10) / 5;
                    for(int i = 0; i < NNickel; i++){
                        resultCoins.add(this.nickelTray.remove(nickelTray.size() - 1));
                    }
                }
            }
        } else if(payment.getChange() >= 10 && payment.getChange() < 25){
            int NDime = payment.getChange() / 10;
            for(int i = 0; i < NDime; i++){
                resultCoins.add(this.dimeTray.remove(dimeTray.size() - 1));
            }

            if((payment.getChange() % 10) >= 5){
                int NNickel = (payment.getChange() % 10) / 5;
                for(int i = 0; i < NNickel; i++){
                    resultCoins.add(this.nickelTray.remove(nickelTray.size() - 1));
                }
            }
        } else if(payment.getChange() >= 5 && payment.getChange() < 10){
            int NNickel = (payment.getChange() % 10) / 5;
            for(int i = 0; i < NNickel; i++){
                resultCoins.add(this.nickelTray.remove(nickelTray.size() - 1));
            }
        }
        return resultCoins;
    }
    
    public void manageCoinStorage(){
        if(nickelTray.size() < 20){
            for (int j = nickelTray.size(); j<50;j++){
                nickelTray.add(new Coin(nickelTray.get(j-1)));
            }
        }else if (nickelTray.size() > 40){
            for (int j = nickelTray.size(); j>25;j--){
                nickelTray.remove(nickelTray.get(j-1));
            }
        }
        
        if(dimeTray.size() < 20){
            for (int j = dimeTray.size(); j<50;j++){
                dimeTray.add(new Coin(dimeTray.get(j-1)));
            }
        }else if (dimeTray.size() > 40){
            for (int j = dimeTray.size(); j>25;j--){
                dimeTray.remove(dimeTray.get(j-1));
            }
        }
        
        if(quarterTray.size() < 20){
            for (int j = quarterTray.size(); j<50;j++){
                quarterTray.add(new Coin(quarterTray.get(j-1)));
            }
        }else if (quarterTray.size() > 40){
            for (int j = quarterTray.size(); j>25;j--){
                quarterTray.remove(quarterTray.get(j-1));
            }
        }
    }
    
    public List<Coin> trash(){
        List<Coin> trash = new ArrayList<>(); 
        for (int i = 0; i<nickelTray.size();i++){
            trash.add(nickelTray.remove(nickelTray.size()-1));
        }
        for (int i = 0; i<dimeTray.size();i++){
            trash.add(dimeTray.remove(dimeTray.size()-1));
        }
        for (int i = 0; i<quarterTray.size();i++){
            trash.add(quarterTray.remove(quarterTray.size()-1));
        }
        return trash;
    }
}
